package com.tbit.uqbike.center.controller;

import com.tbit.uqbike.center.constant.ResponseCode;
import com.tbit.uqbike.center.exception.BaseException;
import com.tbit.uqbike.center.pojo.Machine;
import com.tbit.uqbike.center.pojo.MachineStatus;
import com.tbit.uqbike.center.service.MachineService;
import com.tbit.uqbike.center.service.MachineStatusService;
import com.tbit.uqbike.center.util.Assert;
import com.tbit.uqbike.center.util.OutputUtil;
import com.tbit.uqbike.center.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Controller
@RequestMapping("/machine")
public class MachineController {
    @Autowired
    private MachineService machineService;
    @Autowired
    private MachineStatusService machineStatusService;

    /* 根据设备编号查询 */
    @RequestMapping("/getByMachineNO")
    public void getByMachineNO(String machineNO, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.hasLength(machineNO, "machineNO不能为空");

            Machine machine = machineService.getByMachineNO(machineNO);

            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, machine);
        } catch (BaseException e) {
            ResponseCode.printException(map, e);
        }

        OutputUtil.renderText(request, response, map);
    }

    /* 根据设备状态 */
    @RequestMapping("/getMachineStatus")
    public void getMachineStatus(String machineIds, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.hasLength(machineIds, "machineId不能为空");

            List<Integer> machineIdArray = StringUtil.getIntegerList(machineIds);

            List<MachineStatus> machineStatuses = machineStatusService.getByMachineIds(machineIdArray);

            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, machineStatuses);
        } catch (BaseException e) {
            ResponseCode.printException(map, e);
        }

        OutputUtil.renderText(request, response, map);
    }

}